package api;

import java.util.ArrayList;
import java.util.List;

/**
 * This class helps to construct match info as a well-formed XML string. Structure: <a href="http://pastebin.com/K3JUjD8S">http://pastebin.com/K3JUjD8S</a>
 * 
 * @author Football-Statistics Team
 */
public final class Match {
    private String id;
    private String league;
    private String season;
    private int round;
    private String date;
    private String time;
    private String home;
    private String guests;
    private int score_home_half;
    private int score_guests_half;
    private int score_home_full;
    private int score_guests_full;
    private String referee;
    private String stadium;
    private int spectators;
    private List<Integer> goalsTeam = new ArrayList<Integer>();
    private List<Integer> goalsMin = new ArrayList<Integer>();
    private List<String> goalsPlayer = new ArrayList<String>();
    private List<Integer> cardsTeam = new ArrayList<Integer>();
    private List<Integer> cardsMin = new ArrayList<Integer>();
    private List<String> cardsType = new ArrayList<String>();
    private List<String> cardsPlayer = new ArrayList<String>();
    private List<Integer> changesTeam = new ArrayList<Integer>();
    private List<Integer> changesMin = new ArrayList<Integer>();
    private List<String> changesIn = new ArrayList<String>();
    private List<String> changesOut = new ArrayList<String>();
    private List<String> playersHome = new ArrayList<String>();
    private List<String> playersGuests = new ArrayList<String>();

    /**
     * date setter
     * @param date format DD.MM.YYYY
     */
    public void setDate(String date) {
        this.date = date;
    }

    /**
     * guests team name setter
     * @param guests guest team name
     */
    public void setGuests(String guests) {
        this.guests = guests;
    }

    /**
     * home team name setter
     * @param home home team name
     */
    public void setHome(String home) {
        this.home = home;
    }

    /**
     * match ID setter
     * @param id match ID
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * league name setter
     * @param league league name
     */
    public void setLeague(String league) {
        this.league = league;
    }

    /**
     * referee name setter
     * @param referee referee name
     */
    public void setReferee(String referee) {
        this.referee = referee;
    }

    /**
     * round number setter
     * @param round round number
     */
    public void setRound(int round) {
        this.round = round;
    }

    /**
     * season setter
     * @param season season, format YYYY/YYYY
     */
    public void setSeason(String season) {
        this.season = season;
    }

    /**
     * spectators count setter
     * @param spectators spectators count
     */
    public void setSpectators(int spectators) {
        this.spectators = spectators;
    }

    /**
     * stadium name setter
     * @param stadium stadium name
     */
    public void setStadium(String stadium) {
        this.stadium = stadium;
    }

    /**
     * match start time setter
     * @param time match starting time, format HH:MM
     */
    public void setTime(String time) {
        this.time = time;
    }

    /**
     * score setter
     * @param hh home half-time
     * @param gh guests half-time
     * @param hf home full-time
     * @param gf guests full-time
     */
    public void setScore(int hh, int gh, int hf, int gf) {
        this.score_home_half = hh;
        this.score_guests_half = gh;
        this.score_home_full = hf;
        this.score_guests_full = gf;
    }

    /**
     * Adds goal info
     * @param team team 1-home, 2-guests
     * @param min minute
     * @param player player name
     */
    public void addGoal(int team, int min, String player) {
        this.goalsTeam.add(team);
        this.goalsMin.add(min);
        this.goalsPlayer.add(player);
    }
    
    /**
     * Adds card info
     * @param team team 1-home, 2-guests
     * @param min minute
     * @param type yellow/red
     * @param player player name
     */
    public void addCard(int team, int min, String type, String player) {
        this.cardsTeam.add(team);
        this.cardsMin.add(min);
        this.cardsType.add(type);
        this.cardsPlayer.add(player);     
    }

    /**
     * Adds change info
     * @param team team, 1-home, 2-guests
     * @param min minute
     * @param in player name who steps in
     * @param out player name who steps out
     */
    public void addChange(int team, int min, String in, String out) {
        this.changesTeam.add(team);
        this.changesMin.add(min);
        this.changesIn.add(in);
        this.changesOut.add(out);
    }
    
    /**
     * Adds player to team
     * @param team team, 1-home, 2-guests
     * @param player player name
     */
    public void addPlayer(int team, String player) {
        switch (team) {
            case 1: {
                this.playersHome.add(player);
                break;
            }
            case 2: {
                this.playersGuests.add(player);
                break;
            }    
        }        
    }
    
    /**
     * Returns match info in well-formed XML string, for example structure see <a href="http://pastebin.com/K3JUjD8S">http://pastebin.com/K3JUjD8S</a>
     * 
     * @return well-formed XML string
     */
    public String getXML() {
        String xml = "";
        xml = "<match id=\""+this.id+"\">";
        xml += "<league><![CDATA["+this.league+"]]></league>";
        xml += "<season><![CDATA["+this.season+"]]></season>";
        xml += "<round>"+this.round+"</round>";
        xml += "<date><![CDATA["+this.date+"]]></date>";
        xml += "<time><![CDATA["+this.time+"]]></time>";
        xml += "<home><![CDATA["+this.home+"]]></home>";
        xml += "<guests><![CDATA["+this.guests+"]]></guests>";
        xml += "<score>";
        xml += "<half home=\""+this.score_home_half+"\" guests=\""+this.score_guests_half+"\"/>";
        xml += "<full home=\""+this.score_home_full+"\" guests=\""+this.score_guests_full+"\"/>";
        xml += "</score>";
        xml += "<goals>";
        for (int i = 0; i < this.goalsMin.size(); i++) {
            xml += "<goal team=\""+this.goalsTeam.get(i)+"\" minute=\""+this.goalsMin.get(i) +"\" player=\""+this.goalsPlayer.get(i) +"\"/>";
        }
        xml += "</goals>";
        xml += "<cards>";
        for (int i = 0; i < this.cardsMin.size(); i++) {
            xml += "<card type=\""+this.cardsType.get(i) +"\" team=\""+this.cardsTeam.get(i) +"\" minute=\""+this.cardsMin.get(i) +"\" player=\""+this.cardsPlayer.get(i) +"\"/>";
        }
        xml += "</cards>";
        xml += "<changes>";
        for (int i = 0; i < this.changesMin.size(); i++) {
            xml += "<change team=\""+this.changesTeam.get(i) +"\" minute=\""+this.changesMin.get(i)+"\" in=\""+this.changesIn.get(i)+"\" out=\""+this.changesOut.get(i)+"\"/>";
        }
        xml += "</changes>";
        xml += "<home-team>";
        for (int i = 0; i < this.playersHome.size(); i++) {
            xml += "<player name=\""+this.playersHome.get(i) +"\"/>";
        }
        xml += "</home-team>";
        xml += "<guest-team>";
        for (int i = 0; i < this.playersGuests.size(); i++) {
            xml += "<player name=\""+this.playersGuests.get(i)+"\"/>";
        }
        xml += "</guest-team>";
        xml += "<referee><![CDATA["+this.referee+"]]></referee>";
        xml += "<stadium><![CDATA["+this.stadium+"]]></stadium>";
        xml += "<spectators>"+this.spectators+"</spectators>";
        xml += "</match>";
        return xml;
    }
}
